package com.utils;

import com.domain.entity.UserEntity;
import com.domain.entity.WeixiuyuanEntity;
import io.jsonwebtoken.*;

import java.util.Date;
import java.util.UUID;

/**
 * @author fanshihao
 * @date 2025-03-20 21:32
 */
public class JJwtUtils {


    /**
     * 管理员生成token
     * @return token
     */
    public static String createToken(UserEntity user) {

        String token = Jwts.builder()
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .claim("username", user.getUsername())
                .claim("role", user.getRole())
                .setSubject("auth-token")
                .setExpiration(new Date(System.currentTimeMillis() + 7200 * 1000))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256, "fancy")
                .compact();

        return token;
    }

    /**
     * 维修工生成token
     * @return token
     */
    public static String createToken(WeixiuyuanEntity user) {

        String token = Jwts.builder()
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .claim("id", user.getJobNumber())
                .claim("username", user.getUsername())
                .setSubject("auth-token")
                .setExpiration(new Date(System.currentTimeMillis() + 7200 * 1000))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256, "fancy")
                .compact();

        return token;
    }


}
